package org.jmc.refmanager.web.wicket.page;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.PageParameters;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.jmc.refmanager.util.Util;
import org.jmc.refmanager.web.wicket.model.ServiceModel;
import org.jmc.refmanager.web.wicket.page.panel.ServiceViewPanel;
import org.jmc.refmanager.web.wicket.page.service.AjaxOrgUnitJsonProviderPage;
import org.jmc.refmanager.web.wicket.page.template.TemplatePage;

/**
 */
@AuthorizeInstantiation(value={"ROLE_MODIF", "ROLE_APPROVER"})
public class TestPage extends TemplatePage {
	private WebMarkupContainer viewServicePanel;
	
	/**
	 * 
	 */
	public TestPage() {
		this(null);
	}

	private void replaceEmptyPanel(AjaxRequestTarget target, Panel newPanel) {
		this.viewServicePanel.replaceWith(newPanel);
		this.viewServicePanel = newPanel;
		target.addComponent(newPanel);
	}
	
	/**
	 * 
	 */
	public TestPage(PageParameters parameters) {
		super(parameters);
		add(new Label("pageTitle", "Test"));
		setOutputMarkupId(true);

		add(new MarkupContainer("treeview") {
			private static final long serialVersionUID = 1L;
		});
		
		final AbstractDefaultAjaxBehavior behave = new AbstractDefaultAjaxBehavior() {
			private static final long serialVersionUID = 1L;

			@Override
			protected void respond(final AjaxRequestTarget target) {
				String id = Util.ifNullOrEmpty(getRequest().getParameter("id"), "0");
				Integer serviceId = Integer.valueOf(id);
				if (serviceId != 0) {
					ServiceViewPanel viewServicePanel = new ServiceViewPanel("viewServicePanel", new ServiceModel(serviceId), TestPage.this.getClass());
					viewServicePanel.setOutputMarkupId(true);
	
					replaceEmptyPanel(target, viewServicePanel);
				}
		    }
			
			@Override
	        public void renderHead(IHeaderResponse response) {
	            super.renderHead( response );
	            response.renderOnDomReadyJavascript( getCallbackScript().toString() );
	        }
		};
		add(behave);
		
		viewServicePanel = new WebMarkupContainer("viewServicePanel");
		viewServicePanel.setOutputMarkupId(true);
		add(viewServicePanel);
		
		CharSequence url = RequestCycle.get().urlFor(AjaxOrgUnitJsonProviderPage.class, null);
		StringBuilder script = new StringBuilder();

		script.append("\nfunction getViewServiceUrl() {\n");
		script.append("\nreturn '").append(behave.getCallbackUrl()).append("';\n");
		script.append("};\n");

		script.append("\nfunction getBrowseUrl() {\n");
		script.append("\nreturn '").append(url.toString()).append("&nocache=");
		script.append(Math.random()).append("';\n");
		script.append("};\n");
		
		Label browseUrlScript = new Label("browseUrlScript", script.toString());
		// do not HTML escape JavaScript code
		browseUrlScript.setEscapeModelStrings(false);
		add(browseUrlScript);
	}

}

